<template>
  <div class="app-container">
    <el-form :model="sendform" ref="sendform" :rules="rules" label-width="100px" v-loading="formLoading">
      <el-form-item label="内容：" prop="publishContent">
        <el-input type="textarea" rows="13" v-model="sendform.publishContent"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">发送</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import userApi from '@/api/user'
  import messageApi from '@/api/message'

  export default {
    data() {
      return {
        sendform: {
          id: null,
          publishContent: ''
        },
        formLoading: false,
        rules: {
          publishContent: [{
            required: true,
            message: '请输入回复内容',
            trigger: 'blur'
          }]
        }
      }
    },
    created() {
      this.sendform.id = this.$route.query.id;
    },
    watch: {
      // 监听$route对象的变化来更新查询参数
      '$route': function(to, from) {
        this.sendform.id = to.query.id
      }
    },
    methods: {
      submitForm() {
        let _this = this
        this.$refs.sendform.validate((valid) => {
          if (valid) {
            this.formLoading = true
            messageApi.send(this.sendform).then(data => {
              this.formLoading = false
              this.resetForm()
              this.$router.push('/message/list')
            })
          } else {
            return false;
          }
        });

      },
      resetForm() {
        this.content = ''
      },
    }
  }
</script>
